import {
  Column,
  Entity,
  Index,
  JoinColumn,
  ManyToOne,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { User } from './User';
import { Article } from './Article';

@Index('article_id', ['articleId'], {})
@Index('user_id', ['userId'], {})
@Entity('collection', { schema: 'blog_back' })
export class Collection {
  @PrimaryGeneratedColumn({ type: 'int', name: 'id', unsigned: true })
  id: number;

  @Column('int', {
    primary: true,
    name: 'user_id',
    comment: '用户',
    unsigned: true,
  })
  userId: number;

  @Column('int', {
    primary: true,
    name: 'article_id',
    comment: '文章',
    unsigned: true,
  })
  articleId: number;

  @Column('timestamp', {
    name: 'create_time',
    comment: '创建时间',
    default: () => 'CURRENT_TIMESTAMP',
  })
  createTime: Date;

  @Column('timestamp', {
    name: 'update_time',
    comment: '更新时间',
    default: () => 'CURRENT_TIMESTAMP',
  })
  updateTime: Date;

  @ManyToOne(() => User, (user) => user.collections, {
    onDelete: 'NO ACTION',
    onUpdate: 'NO ACTION',
  })
  @JoinColumn([{ name: 'user_id', referencedColumnName: 'id' }])
  user: User;

  @ManyToOne(() => Article, (article) => article.collections, {
    onDelete: 'NO ACTION',
    onUpdate: 'NO ACTION',
  })
  @JoinColumn([{ name: 'article_id', referencedColumnName: 'id' }])
  article: Article;
}
